package ua.kharkov.knure.mikhaylov.st4.web.command;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import ua.kharkov.knure.mikhaylov.st4.db.entities.User;
import ua.kharkov.knure.mikhaylov.st4.db.enums.Role;
import ua.kharkov.knure.mikhaylov.st4.db.management.DBManager;
import ua.kharkov.knure.mikhaylov.st4.settings.LocaleData;
import ua.kharkov.knure.mikhaylov.st4.settings.Path;

/**
 * Command - registers a new user
 * 
 * @author Saint
 * 
 */
public class RegisterCommand extends Command {

	private static final long serialVersionUID = -8341416720184248850L;
	private static final Logger log = Logger.getLogger(RegisterCommand.class);

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {

		log.debug("Command starts");

		log.debug("Checking parameters");

		String errorMessage = null;
		String forward = Path.PAGE_ERROR;

		String login = request.getParameter("login");
		String password = request.getParameter("password");
		String passwordRepeat = request.getParameter("passwordRepeat");
		String firstName = request.getParameter("firstName");
		String lastName = request.getParameter("lastName");

		if (login == null || password == null || passwordRepeat == null
				|| firstName == null || lastName == null) {
			errorMessage = LocaleData.getResourceBundle().getString(
					"servlet.error.data.empty");
			request.setAttribute("errorMessage", errorMessage);
			log.error("errorMessage --> " + errorMessage);
			return forward;
		}

		if (login.length() < 6 || login.length() > 20 || password.length() < 6
				|| password.length() > 20 || firstName.length() == 0
				|| firstName.length() > 20 || lastName.length() == 0
				|| lastName.length() > 20) {
			errorMessage = LocaleData.getResourceBundle().getString(
					"servlet.error.data.bad");
			request.setAttribute("errorMessage", errorMessage);
			log.error("errorMessage --> " + errorMessage);
			return forward;
		}

		if (!password.equals(passwordRepeat)) {
			errorMessage = LocaleData.getResourceBundle().getString(
					"servlet.error.password.match");
			request.setAttribute("errorMessage", errorMessage);
			log.error("errorMessage --> " + errorMessage);
			return forward;
		}

		log.debug("Checking user existance");

		if (DBManager.getInstance().findUserByLogin(login) != null) {
			errorMessage = "Such user already exists";
			request.setAttribute("errorMessage", errorMessage);
			log.error("errorMessage --> " + errorMessage);
			return forward;
		}

		User user = new User();
		user.setLogin(login);
		user.setPassword(password);
		user.setFirstName(firstName);
		user.setLastName(lastName);
		user.setRole(Role.CLIENT);

		log.debug("Inserting user");

		DBManager.getInstance().insertUser(user);

		log.debug("Command finished");

		request.setAttribute("message", LocaleData.getResourceBundle()
				.getString("servlet.success.user.create"));
		request.setAttribute("next", Path.PAGE_LOGIN);
		forward = Path.PAGE_SUCCESS;
		return forward;
	}

}
